<template>
	<view class="mainbgc" v-if="pageShow">
		<header-top :messageInfo="{typeFlag:true,message:'出库单详情',liner:false}">
			<view class="rightmsg" slot="rightmsg">
				<view class="rightmsg_content flexleft" @click="startPrint(114)">
					<image class="img" src="../../../static/image/system/my2.png" mode=""></image>
				</view>
			</view>
		</header-top>
		<view class="mainpadding_top flexbetween ffffff">
			<view class="titletext">{{dataAll.order_no}}</view>
			<view class="wlable radius" v-show="dataAll.status==10">待出库</view>
			<view class="green radius" v-show="dataAll.status==20">已出库</view>
			<view class="hlable radius" v-show="dataAll.status==30">入库拒绝</view>
		</view>
		<view class="mainpadding_top ffffff">
			<view class="flexleft margin_top paddingbom">
				<view class="nostrongtext">创建时间：</view>
				<view class="strongtext">{{dataAll.createtime_text}}</view>
			</view>
			<view class="flexleft margin_top paddingbom">
				<view class="nostrongtext">经办人：</view>
				<view class="strongtext">{{dataAll.staff.username}}</view>
			</view>
			<view class="flexleft margin_top paddingbom" @click.stop="phone(dataAll.staff.mobile)">
				<view class="nostrongtext">经办人电话：</view>
				<view class="strongtext">{{dataAll.staff.mobile}}</view>
				<view class="dizhiimg"></view>
			</view>
			<view class="flexleft margin_top paddingbom">
				<view class="nostrongtext">入库仓库：</view>
				<view class="strongtext">{{dataAll.warehouse.name}}</view>
			</view>
			<view class="flexleft margin_top paddingbom">
				<view class="nostrongtext">出库类型：</view>
				<view class="strongtext">{{leixing(dataAll.type)}}</view>
			</view>
			<view class="flexleft margin_top paddingbom" v-if="dataAll.supplier.id">
				<view class="nostrongtext">供应商名称：</view>
				<view class="strongtext">{{dataAll.supplier.name}}</view>
			</view>
			<view class="flexleft margin_top paddingbom" v-if="dataAll.supplier.id"  @click.stop="phone(dataAll.supplier.mobile)">
				<view class="nostrongtext">供应商电话：</view>
				<view class="strongtext">{{dataAll.supplier.mobile}}</view>
				<view class="dizhiimg"></view>
			</view>
			<view class="flexleft margin_top paddingbom" v-if="dataAll.custom.id">
				<view class="nostrongtext">客户名称：</view>
				<view class="strongtext">{{dataAll.custom.name}}</view>
			</view>
			<view class="flexleft margin_top paddingbom" v-if="dataAll.custom.id"  @click.stop="phone(dataAll.custom.mobile)">
				<view class="nostrongtext">客户电话：</view>
				<view class="strongtext">{{dataAll.custom.mobile}}</view>
				<view class="dizhiimg"></view>
			</view>
			<view class="flexleft margin_top paddingbom" v-if="dataAll.ps_type">
				<view class="nostrongtext">配送方式：</view>
				<view class="strongtext">{{dataAll.ps_type==1?"自提":"送货"}}</view>
			</view>
			<view class="flexleft margin_top paddingbom" v-if="dataAll.send_staffinfo">
				<view class="nostrongtext">司机：</view>
				<view class="strongtext">{{dataAll.send_staffinfo.username}}</view>
			</view>
			<view class="flexleft margin_top" v-if="dataAll.send_staffinfo"  @click.stop="phone(dataAll.send_staffinfo.mobile)">
				<view class="nostrongtext">司机电话：</view>
				<view class="strongtext">{{dataAll.send_staffinfo.mobile}}</view>
				<view class="dizhiimg"></view>
			</view>
		</view>
		<view class="mainpadding_top ffffff">
			<view class="flexbetween">
				<view class="titletext" style="flex:4;">商品名称</view>
				<view class="titletext flexcenter" style="flex: 3;">规格</view>
				<view class="titletext flexright" style="flex: 2;">数量</view>
			</view>
			<view class="paddingbom" v-for="item in dataAll.ordergoods" :key="item.id">
				<view class="flexstart margin_top2 ">
					<view class="nostrongtext ywy" style="flex: 4;">{{item.goods_info.name}}</view>
					<view class="nostrongtext flexcenter" style="flex: 3;" v-show="item.difference_type==1">{{item.goods_info.biggoodsunit.name}}</view>
					<view class="nostrongtext flexcenter" style="flex: 3;" v-show="item.difference_type==2">{{item.goods_info.middlegoodsunit.name}}</view>
					<view class="nostrongtext flexcenter" style="flex: 3;" v-show="item.difference_type==3">{{item.goods_info.smallgoodsunit.name}}</view>
					<view class="nostrongtext flexright" style="flex: 2;">*{{item.num}}</view>
				</view>
				<view class="flexbetween margin_topx" v-show="item.goods_type==1 && item.notes">
					<view class="huisetext">备注：{{item.notes}}</view>
				</view>
			</view>
			<view class="flexbetween margin_top">
				<view class="titletext">合计</view>
				<view class="titletext flexright">共{{dataAll.goods_zl_num}}种({{dataAll.goods_num}})</view>
			</view>
		</view>
		<view class="flexleft mainpadding" v-if="dataAll.notes">
			<view class="nostrongtext">备注：</view>
			<view class="strongtext">{{dataAll.notes}}</view>
		</view>
		<moDal :message="{modalShow:modal.modalShow,title:modal.title}" @modalSure="modalSure"></moDal><!-- 模态框 -->
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	const tsc = require("@/util/ble/tsc.js");
	const esc = require("@/util/ble/esc.js");
	const encode = require("@/util/ble/encoding.js");
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				listid: "",
				dataAll: {},
				// 模态框
				modal: {
					modalShow: false,
					title: "",
				},
				pageShow:false,
				// 打印机技术参数
				sendContent: "../../../static/image/news/boy.png",
				looptime: 0,
				currentTime: 1,
				lastData: 0,
				oneTimeData: 0,
				returnResult: "",
				canvasWidth: 180,
				canvasHeight: 180,
				imageSrc: '../../static/img/abc_ic_star_black_16dp.png',
				buffSize: [],
				buffIndex: 0,
				printNum: [],
				printNumIndex: 0,
				printerNum: 1,
				currentPrint: 1,
				isReceiptSend: false,
				isLabelSend: false,
				// 打印机技术结束
			}
		},
		onLoad(options) {
			this.listid = options.id
			this.init()
		// 打印机
			let that = this;
			let {
				BLEInformation
			} = that.Bluetooth;
			// 打印机结束
		},
		// 打印机开始
		computed: mapState(['sysinfo', 'Bluetooth']),
		onReady() {
			let list = []
			let numList = []
			let j = 0
			for (let i = 20; i < 200; i += 10) {
				list[j] = i;
				j++
			}
			for (let i = 1; i < 10; i++) {
				numList[i - 1] = i
			}
			this.buffSize = list;
			this.oneTimeData = list[0];
			this.printNum = numList;
			this.printerNum = numList[0];
		},
		onUnload() {
			let that = this;
			let {
				BLEInformation
			} = that.Bluetooth;
		},
		// 打印机结束
		onShow() {
			// #ifdef MP-WEIXIN || H5
			this.init()
			// #endif
			// 打印机开始
			let that = this;
			let width;
			let height;
			uni.getImageInfo({
				src: that.imageSrc,
				success(res) {
					console.log(res.width)
					console.log(res.height)
					width = res.width
					height = res.height
					that.canvasWidth = res.width;
					that.canvasHeight = res.height;
				}
			})
			const ctx = uni.createCanvasContext("edit_area_canvas", this);
			ctx.drawImage(this.imageSrc, 0, 0, width, height);
			ctx.draw();
			// 打印机结束
		},
		methods: {
			// 打印机开始
			receiptTest() {
				// this.selectshow = false
				var that = this;
				var canvasWidth = that.canvasWidth
				var canvasHeight = that.canvasHeight
				var command = esc.jpPrinter.createNew()
				command.init()
				// 标题
				command.bold(1); //加粗
				command.setFontSize(16); //字体大小
				command.setSelectJustification(1) //居中0左对齐1居中对齐2右对齐
				command.rowSpace(100);
				command.setText("出库单");
				command.setPrint();
				command.rowSpace(60); //行间距
				command.bold(0); //取消加粗
				command.setFontSize(0); //正常字体
				// 内容
				command.bold(0); //加粗
				command.setSelectJustification(0) //居中0左对齐1居中对齐2右对齐
				command.setText("订单时间：" + that.dataAll.createtime_text);
				command.setPrint();
				command.setText("订单编号：" + that.dataAll.order_no);
				command.setPrint();
				let custom_name = that.dataAll.custom.name?that.dataAll.custom.name:"无"
				let custom_mobile = that.dataAll.custom.mobile?that.dataAll.custom.mobile:"无"
				let custom_address = that.dataAll.custom.address?that.dataAll.custom.address:"无"
				command.setText("客户：" + custom_name + "  客户电话：" + custom_mobile);
				command.setPrint();
				command.setText("客户地址：" +custom_address);
				command.setPrint();
				let sendstaff = that.dataAll.send_staffinfo?that.dataAll.send_staffinfo.username+"（"+that.dataAll.send_staffinfo.mobile+"）" :"无"
				command.setText("司机：" + sendstaff + "  仓库："+that.dataAll.warehouse.name);
				command.setPrint();
				command.setText("经办人：" + that.dataAll.staff.username+"（"+that.dataAll.staff.mobile+"）");
				command.setPrint();
				command.setText("类型：" + that.leixing(that.dataAll.type));
				command.setPrint();
				let supplier_name = that.dataAll.supplier.name?that.dataAll.supplier.name:"无"
				let supplier_mobile = that.dataAll.supplier.mobile?that.dataAll.supplier.mobile:"无"
				command.setText("供应商名称：" + supplier_name + "  供应商电话：" + supplier_mobile);
				command.setPrint();
				let yw_staffinfo = that.dataAll.yw_staffinfo?that.dataAll.yw_staffinfo.username+"（"+that.dataAll.yw_staffinfo.mobile+"）" :"无"
				command.setText("业务：" + yw_staffinfo);
				command.setPrint();
				// 表格
				command.rowSpace(80); //间距
				command.bold(5); //加粗
				command.setText("商品名称");
				command.setAbsolutePrintPosition(270);
				command.setText("规格");
				command.setAbsolutePrintPosition(380);
				command.setText("数量");
				command.setPrint()
				command.bold(0); //加粗
				that.dataAll.ordergoods.forEach(item=>{
					command.setText(item.goods_info.name);
					command.setAbsolutePrintPosition(270);
					if(item.difference_type==1){
						command.setText(item.goods_info.biggoodsunit.name);
					}else if(item.difference_type==2){
						command.setText(item.goods_info.middlegoodsunit.name);
					}else{
						command.setText(item.goods_info.smallgoodsunit.name);
					}
					command.setAbsolutePrintPosition(380);
					command.setText(item.num);
					command.setPrint()
				})
				command.bold(5); //加粗
				command.setText("种类:" + that.dataAll.goods_zl_num+"  数量:"+that.dataAll.goods_num);
				command.setPrint()
				command.setText("订单备注："+that.dataAll.notes);
				command.setPrint()
				command.setText("签字：");
				command.setPrint()
				command.setSelectJustification(1) //居中0左对齐1居中对齐2右对齐
				command.setText("询博技术支持");
				command.setPrintAndFeedRow(3);
				that.isReceiptSend = true;
				that.prepareSend(command.getData());
			},
			
			//准备发送，根据每次发送字节数来处理分包数量
			prepareSend(buff) {
				console.log(buff);
				let that = this
				let time = that.oneTimeData
				let looptime = parseInt(buff.length / time);
				let lastData = parseInt(buff.length % time);
				console.log(looptime + "---" + lastData)
				this.looptime = looptime + 1;
				this.lastData = lastData;
				this.currentTime = 1;
				that.Send(buff)
			},
			//分包发送
			Send(buff) {
				let that = this
				let {
					currentTime,
					looptime: loopTime,
					lastData,
					oneTimeData: onTimeData,
					printerNum: printNum,
					currentPrint
				} = that;
				let buf;
				let dataView;
				if (currentTime < loopTime) {
					buf = new ArrayBuffer(onTimeData)
					dataView = new DataView(buf)
					for (var i = 0; i < onTimeData; ++i) {
						dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
					}
				} else {
					buf = new ArrayBuffer(lastData)
					dataView = new DataView(buf)
					for (var i = 0; i < lastData; ++i) {
						dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
					}
				}
				console.log("第" + currentTime + "次发送数据大小为：" + buf.byteLength)
				let {
					BLEInformation
				} = that.Bluetooth;
			
				plus.bluetooth.writeBLECharacteristicValue({
					deviceId: BLEInformation.deviceId,
					serviceId: BLEInformation.writeServiceId,
					characteristicId: BLEInformation.writeCharaterId,
					value: buf,
					success: function(res) {
						console.log(res)
					},
					fail: function(e) {
						console.log(e)
					},
					complete: function() {
						currentTime++
						if (currentTime <= loopTime) {
							that.currentTime = currentTime;
							that.Send(buff)
						} else {
							//  uni.showToast({
							// title: '已打印第' + currentPrint + '张',
							//  })
							if (currentPrint == printNum) {
								that.looptime = 0;
								that.lastData = 0;
								that.currentTime = 1;
								that.isReceiptSend = false;
								that.isLabelSend = false;
								that.currentPrint = 1;
							} else {
								currentPrint++;
								that.currentPrint = currentPrint;
								that.currentTime = 1;
								that.Send(buff)
							}
						}
					}
				})
			},
			// 打印机结束
			init() {
				httpRequest.request('/dhtapi/Outboundorder/detail', 'GET', {
					id: this.listid
				}, false, false, true).then(res => {
					this.dataAll = res.data
					this.pageShow = true
				})
			},
			leixing(type) {
				// 1=采购退货出库单,2=销售单出库单,3=其他入库单
				if(type==1){
					return "采购退货出库单,"
				} else if(type==2){
					return "销售单出库单"
				} else if(type==3){
					return "其他入库单"
				}
			},
			startPrint(qxpdid) {
				if (!httpRequest.qxpd(qxpdid, this.$store.state.userInfo.ability_rule_ids)) {
					return false
				}
				let that = this
				uni.showActionSheet({
					itemList: ['云打印机', '蓝牙打印机'],
					success: function(res) {
						// 选择云打印机走0
						if (res.tapIndex == 0) {
							that.modal.title = "请再次确认打印！"
							that.modalSure()
						}
						// 选择蓝牙打印机走1
						if (res.tapIndex == 1) {
							that.receiptTest()
						}
					},
					fail: function(res) {}
				});
			},
			modalSure(){
				this.modal.modalShow = false
				if(this.modal.title=="请再次确认打印！"){
					uni.showLoading({
						mask:true,
						title:"发送数据中"
					})
					httpRequest.request("/dhtapi/Outboundorder/dyOutboundorderDetail", 'GET', {
						id:this.listid,
					}, false, false, true).then(res => {
						uni.hideLoading()
						httpRequest.toast(res.msg)
					}).catch(err => {
						httpRequest.toast(err.msg);
					})
				}
			},
			phone(p) {
				uni.makePhoneCall({
					phoneNumber: p.toString()
				}).catch((e) => {
					// console.log(e)  //用catch(e)来捕获错误{makePhoneCall:fail cancel}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	// 审核状态
	.hlable {
		padding: 6rpx 14rpx;
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #ff0000;
		line-height: 28rpx;
		background-color: rgba(253, 4, 4, .1);
	}

	.wlable {
		padding: 6rpx 14rpx;
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #F3AD3D;
		line-height: 28rpx;
		background-color: #fef7ec;
	}

	.green {
		padding: 6rpx 14rpx;
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #0ad161;
		line-height: 28rpx;
		background-color: rgba(10, 209, 97, .1);
	}

	.paddingbom {
		padding-bottom: 30rpx;
		border-bottom: 1rpx solid rgba(153, 153, 153, 0.03);
	}

	.paddingbom:last-child {
		padding-bottom: 0;
		border: none;
	}

	.maincolor_text {
		color: #0BBDCB;
	}

	// 表格内容
	.flexcolumnleft {
		display: flex;
		align-items: flex-start;
		justify-content: flex-start;
		flex-direction: column;
	}

	.flexcolumnright {
		display: flex;
		align-items: flex-end;
		justify-content: flex-end;
		flex-direction: column;
	}

	.smalltext {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}

	.heji {
		padding: 30rpx 30rpx 156rpx;
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #000000;
	}
</style>